package org.linlinjava.litemall.db.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.db.dao.AgedmallAgentMapper;
import org.linlinjava.litemall.db.dao.AgedmallDoctorInfoMapper;
import org.linlinjava.litemall.db.dao.AgedmallHospitalSendSelfLocationMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.app.AppAgentMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.AgentConstant;
import org.linlinjava.litemall.db.vo.app.FinanceDetailDayVo;
import org.linlinjava.litemall.db.vo.app.FinanceDetailMonthVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AgedmallAgentService {
    @Resource
    private AgedmallAgentMapper agedmallAgentMapper;
    @Resource
    private AppAgentMapper appAgentMapper;
    @Resource
    private AgedmallDoctorInfoMapper agedmallDoctorInfoMapper;
    @Resource
    private LitemallUserMapper litemallUserMapper;

    public AgedmallAgent getDoctorByUid(Integer userId) {
        AgedmallAgentExample example = new AgedmallAgentExample();
        example.or().andDeletedEqualTo(false)
                .andTypeEqualTo(AgentConstant.TYPE_DOCTOR)
                .andUserIdEqualTo(userId);
        return agedmallAgentMapper.selectOneByExample(example);
    }

    public AgedmallAgent getCommonByUid(Integer userId) {
        AgedmallAgentExample example = new AgedmallAgentExample();
        example.or()//.andDeletedEqualTo(false)
                //.andTypeEqualTo(AgentConstant.TYPE_COMMON)
                //.andUserIdEqualTo(userId);
                .andIdEqualTo(userId);
        return agedmallAgentMapper.selectOneByExample(example);
    }

    public AgedmallAgent getById(Integer id) {
        return agedmallAgentMapper.selectByPrimaryKey(id);
    }

    public List<AgedmallAgent> getByUid(Integer userId) {
        AgedmallAgentExample example = new AgedmallAgentExample();
        example.or().andDeletedEqualTo(false)
                .andUserIdEqualTo(userId);
        return agedmallAgentMapper.selectByExample(example);
    }

    public List<FinanceDetailMonthVo> getFinanceDetailMonthVo(Integer userId, Integer year) {
        return appAgentMapper.getFinanceDetailMonthVo(userId, year);
    }

    public List<FinanceDetailDayVo> getFinanceDetailDayVo(Integer userId, Integer year, Integer month) {
        return appAgentMapper.getFinanceDetailDayVo(userId, year, month);
    }

    public List<String> getDeliveryRoles(Integer userId){
        List<String> backString=new ArrayList<>();
        List<String> rolesStrList = appAgentMapper.getdeliveryRole(userId);
        AgedmallDoctorInfoExample doctorInfoExample=new AgedmallDoctorInfoExample();
        doctorInfoExample.or().andUserIdEqualTo(userId.longValue()).andDeletedEqualTo(false);
        AgedmallDoctorInfo agedmallDoctorInfo = agedmallDoctorInfoMapper.selectOneByExample(doctorInfoExample);
        if(ObjectUtil.isNotNull(agedmallDoctorInfo)){
            backString.add("doctor");
        }
        //增加导医台角色
        LitemallUser litemallUser=litemallUserMapper.selectByPrimaryKey(userId);
        AgedmallHospitalSendSelfLocation hospitalSendSelfLocation = appAgentMapper.selectUserIsHaveHosGuider(litemallUser.getMobile());
        if(ObjectUtil.isNotNull(hospitalSendSelfLocation)){
            backString.add("HosGuider");
        }

        if(rolesStrList.size() == 0) {
            return backString;
        }
        Set<String> roleSet = new HashSet<>();
        rolesStrList.stream().forEach(r->{
            if(StringUtils.isNotBlank(r)) {
                roleSet.addAll(Arrays.asList(r.split(",")));
            }
        });
        for (String r:roleSet) {
            switch (r){
                case "0":
                    //配货员
                    backString.add("packer");
                    break;
                case "1":
                    //配送员
                    backString.add("expressman");
                    //取货员
                    backString.add("picker");
                    break;
                case "2":
                    //维护员
                    backString.add("repairman");
                    break;
                default:
                    break;
            }
        }
        return backString;
    }
}
